<?php
/**
 * compoundsave
 * 保存情况
 * @author Administrator
 * @version
 */
class Model_Tables_Compoundsave extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'compoundsave';
    /**
     * add
     * 增加保存情况
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'compoundid' => $params['compoundid'],
		'orderid' => $params['orderid'],
		'batch' => $params['batch'],
		'purity' => $params['purity'],
		'container' => $params['container'],
        'condition' => $params['condition'],
		'location' => $params['location'],
        'num' => $params['num'],
        'dict1' => $params['dict1'],
		'producedate' => $params['producedate'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑保存情况
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'compoundid' => $params['compoundid'],
		'orderid' => $params['orderid'],
		'batch' => $params['batch'],
		'purity' => $params['purity'],
		'container' => $params['container'],
        'condition' => $params['condition'],
		'location' => $params['location'],
        'num' => $params['num'],
        'dict1' => $params['dict1'],
		'producedate' => $params['producedate'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑保存情况
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转保存情况
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update compoundsave set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除保存情况
     * @param $id 主键值
     */
    public function del( $id )
    {
     	//$where = 'id = '.$id;
     	
     	$db = $this->getAdapter();
     	$sql = "update compoundsave set  isdelete =1 where id=:id";
     	return $db->query($sql,array("id"=>$id));
       // return $this->delete($where);
    }
    /**
     * getInfo
     * 获取保存情况信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取保存情况数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "",$compoundid=-1)
    { 
    	$db = $this->getAdapter();
    	$compoundid = intval($compoundid);
    	$where = $db->quoteInto("isdelete=0 and compoundid=?", $compoundid);
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
			$where .= $db->quoteInto(" and id=?", $s);
    	}
    	$select = $select->where($where);
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取保存情况数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "",$compoundid=-1)
    {
    	$db = $this->getAdapter();
    	$compoundid = intval($compoundid);
    	$where = $db->quoteInto("isdelete=0 and compoundid=?", $compoundid);
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$where .= $db->quoteInto(" and id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    public function getnewid($compoundid){
    	$sql = "select count(*)+1 as id from compoundsave where compoundid=:compoundid";
    	$id = $this->getAdapter()->fetchOne($sql,array("compoundid"=>$compoundid));
    	$id += 1000;
    	return substr($id,1);
    }
    public function getCompoundSaveList($compoundid)
    {
    	$result = $this->getList(1,100,"",$compoundid);
    	$compoundsave = array();
    	if (!empty($result))
    	{
    		foreach ($result as $item)
    		{
    			$compoundsave[$item['id']] = $item['batch'];
    		}
    	}
    	return $compoundsave;
    }	
}